package org.dashboard.utils;

import javax.script.*;

/**
 * Copyright © 2018. All rights reserved.
 *
 * @Author: FreezeSoul
 * @CreateDate: 2018/8/13 17:48
 * @Description: JavaScriptUtil
 * @Version: V1.0
 */
public class JavaScriptUtil {

    public static String execute(String script, String name, Object params) throws ScriptException, NoSuchMethodException {

        final ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
        final Compilable compilable = (Compilable) engine;
        final Invocable invocable = (Invocable) engine;

        final CompiledScript compiled = compilable.compile(script);

        compiled.eval();

        return invocable.invokeFunction(name, params).toString();
    }
}
